home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / stackary.arc / STACKARY.PAS < prev    next >
Pascal/Delphi Source File  |  1991-01-29  |  4KB  |  148 lines

  1. UNIT StackAry;
  2.  
  3. (*
  4.       Library of Stack Array routines.
  5.  
  6.       by: Ilya Shlyakhter
  7. *)
  8.  
  9.                               INTERFACE
  10.  
  11.    CONST MaxStackSize = 100;
  12.  
  13.  
  14.    TYPE  StackDataType = Char;
  15.          StackIndexType = 0..MaxStackSize;
  16.          StackArray = ARRAY [1..MaxStackSize] OF StackDataType;
  17.  
  18.          StackType = RECORD
  19.                         StackData: StackArray;
  20.                         StackTop: StackIndexType
  21.                      END;  (* StackType *)
  22.  
  23.    PROCEDURE InitStack (VAR Stack: StackType);
  24.  
  25.    FUNCTION Empty (VAR Stack: StackType): Boolean;
  26.    FUNCTION GetStackSize (VAR Stack: StackType): StackIndexType;
  27.  
  28.    PROCEDURE Push (VAR Stack: StackType; 
  29.                    NewData: StackDataType;
  30.                    VAR Error: Boolean);
  31.  
  32.    PROCEDURE Pop (VAR Stack: StackType; 
  33.                   VAR TopElement: StackDataType;
  34.                   VAR Error: Boolean);
  35.  
  36.    PROCEDURE Peek (VAR Stack: StackType; 
  37.                    VAR TopElement: StackDataType;
  38.                    VAR Error: Boolean);
  39.  
  40.  
  41.                             IMPLEMENTATION
  42.  
  43.    PROCEDURE InitStack (VAR Stack: StackType);
  44.  
  45.    (*
  46.       Clears a given stack (Stack), preparing it to receive elements.
  47.    *)
  48.  
  49.       VAR I: StackIndexType;
  50.  
  51.       BEGIN  (* InitStack *)
  52.          Stack.StackTop := 0
  53.       END;   (* InitStack *)
  54.  
  55.  
  56.    FUNCTION Empty (VAR Stack: StackType): Boolean;
  57.  
  58.    (*
  59.       Returns TRUE if there are no elements of the stack (Stack), FALSE otherwise.
  60.    *)
  61.  
  62.       BEGIN  (* Empty *)
  63.          Empty := (Stack.StackTop = 0)
  64.       END;   (* Empty *)
  65.  
  66.    FUNCTION GetStackSize (VAR Stack: StackType): StackIndexType;
  67.  
  68.    (*
  69.       Returns the number of elements on a given stack (Stack).  Returns 0
  70.       if Stack is empty.
  71.    *)
  72.  
  73.       BEGIN  (* GetStackSize *)
  74.          GetStackSize := Stack.StackTop
  75.       END;   (* GetStackSize *)
  76.  
  77.    PROCEDURE Push (VAR Stack: StackType; 
  78.                    NewData: StackDataType;
  79.                    VAR Error: Boolean);
  80.  
  81.    (*
  82.       Puts the element (NewData) onto the stack (Stack).  If the stack is full,
  83.       leaves the stack intact and returns an error (sets Error to True).
  84.    *)
  85.  
  86.       BEGIN  (* Push *)
  87.          IF Stack.StackTop < MaxStackSize THEN
  88.             BEGIN  (* there is more space on the stack *)
  89.                Stack.StackTop := Stack.StackTop + 1;
  90.                Stack.StackData [Stack.StackTop] := NewData;
  91.                Error := False;
  92.             END    (* there is more space on the stack *)
  93.                ELSE
  94.                   Error := True  (* stack full *)
  95.       END;   (* Push *)
  96.  
  97.  
  98.    PROCEDURE Pop (VAR Stack: StackType; 
  99.                   VAR TopElement: StackDataType;
  100.                   VAR Error: Boolean);
  101.  
  102.    (*
  103.       Retrievs an element (TopElements) from the stack (Stack).  If the stack
  104.       is empty, returns an error (sets Error to True).
  105.    *)
  106.  
  107.       BEGIN  (* Pop *)
  108.          IF Stack.StackTop > 0 THEN
  109.             BEGIN  (* there is at least 1 element on the stack *)
  110.                TopElement := Stack.StackData [Stack.StackTop];
  111.                Stack.StackTop := Stack.StackTop - 1;
  112.                Error := False
  113.             END    (* there is at least 1 element on the stack *)
  114.                ELSE
  115.                   Error := True   (* stack empty *)
  116.       END;   (* Pop *)
  117.  
  118.    PROCEDURE Peek (VAR Stack: StackType; 
  119.                    VAR TopElement: StackDataType;
  120.                    VAR Error: Boolean);
  121.  
  122.    (*
  123.       Returns an element (TopElement) at the top of the stack (Stack), but
  124.       does NOT remove it from the stack, so the next call to Push will
  125.       return this element.  This allows the program to "look ahead" before
  126.       retrieving the next data item.  If the stack is empty, returns an error
  127.       (sets Error to True).
  128.    *)
  129.  
  130.       BEGIN  (* Peek *)
  131.          IF Stack.StackTop > 0 THEN
  132.             BEGIN  (* there is at least 1 element on the stack *)
  133.                TopElement := Stack.StackData [Stack.StackTop];
  134.                Error := False
  135.             END    (* there is at least 1 element on the stack *)
  136.                ELSE
  137.                   Error := True   (* stack empty *)
  138.       END;   (* Peek *)
  139.  
  140.    END.  (* of Unit StackAry *)
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.